package siteflash.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import siteflash.data.SiteFlashPersistenceManage;
import siteflash.helper.Paginator;
import siteflash.model.Game;
import siteflash.model.Paginate;
import siteflash.model.Tag;

import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import com.google.apphosting.utils.remoteapi.RemoteApiPb.Request;

public class Home extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws IOException {
        req.setAttribute("title", "Y1S Game Online Directory, Game Flash Directory");
        req.setAttribute("keyword", "y1s, y1s.org, game directory, flash games, free flash games, online games, play game, fun, flash games online");
        req.setAttribute("description", "Y1s.org Flash Games Directory, Flash Online Game Directory");
        
        PersistenceManager psm = SiteFlashPersistenceManage.get().getPersistenceManager();
        //List all tags
        ArrayList<Tag> tags = (ArrayList<Tag>) General.getTags();
        int total = (int) General.getTotal(tags);
        
        //Set limit and number page
        int limit = 18;
        String strPage = (String) req.getParameter("page");
        int page = 1;
        if (strPage != null) {
        	page = Integer.parseInt(strPage);
        }
        int start = (page - 1) * limit;
        int end = page * limit;
        
        Cache cache=null;
		String cachename = "listgame_" + strPage;
		ArrayList<Game> games = new ArrayList<Game>();
		try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
           e.printStackTrace();
        }
        
        if (cache.containsKey(cachename)) {
        	games = (ArrayList<Game>) cache.get(cachename);
        	System.out.println("List Game: cache print");
        } else {
	        //List all game
	        Query query = psm.newQuery(Game.class);
	        query.setOrdering("name asc");
	        query.setRange(start, end);
	        List<Game> listResult = (List<Game>) query.execute();
	        for (int i=0; i < listResult.size(); i++) {
	            games.add(listResult.get(i));
	        }
	        cache.put(cachename, games);
	        System.out.println("List game: Data print " + cachename);
        }
        //List hot game
        ArrayList<Game> hotGames = (ArrayList<Game>) General.hotGames();
        
        //Paginate
        Paginator paginator = new Paginator();
        paginator.setLimit(limit);
        Paginate paginate = paginator.paginate(page, total);
        
        req.setAttribute("pageName", "home");
        req.setAttribute("tags", tags);
        req.setAttribute("games", games);
        req.setAttribute("hotGame", hotGames);
        req.setAttribute("paginate", paginate);
        try {
            req.getRequestDispatcher("/pages/homepage.jsp").forward(req, resp);
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
    }
}
